home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0885.arc
/
NIAL2.LTG
< prev
next >
Wrap
Text File
|
1986-02-27
|
2KB
|
50 lines
Nial Listing 2
Sieve of Eratosthenes
sieve IS OPERATION N {
Candidates := rest count N;
Primes := Null;
WHILE not empty Candidates DO
Primes := Primes append first Candidates;
Candidates := not (Candidates mod first Candidates match 0)
sublist Candidates;
ENDWHILE;
Primes }
Explanation:
count N gives array of numerals from 1 through N.
rest count N gives array of numerals from 2 through N.
Primes := Null initializes array Primes to empty array.
Candidates mod first Candidates
gives array of remainders after dividing
elements of Candidates by first element.
Candidates ... match 0 gives a bitstring true for elements which
match 0, false otherwise.
not ... match 0) gives bitstring with true changed to false
and false to true.
Candidates := ... sublist Candidates
gives an array which is a sublist of
Candidates consisting of the elements
of Candidates which correspond to the
true bits in the bitstring, and assigns it
to Candidates.
Primes := Primes append first Candidates
appends the first element of the array
Candidates to the array Primes.
Thσ WHIL┼ loo≡ start≤ witΦ aε arra∙ calleΣ Candidate≤, ì
consistinτ oµ thσ numeral≤ froφ ▓ througΦ N« I⌠ firs⌠ append≤ ▓ ì
t∩ thσ arra∙ Primes¼ then replace≤ Candidate≤ witΦ aε arra∙ ì
consistinτ oµ thσ element≤ oµ thσ previous arra∙ no⌠ divisiblσ b∙ ì
thσ firs⌠ elemen⌠ oµ tha⌠ previou≤ array¼ initiall∙ 2, anΣ append≤ ì
thσ firs⌠ elemen⌠ oµ tha⌠ ne≈ arra∙ t∩ thσ arra∙ Primes« Thσ ì
loop terminate≤ wheε n∩ morσ element≤ remaiε iε thσ arra∙ ì
Candidates« Oε ß Fortunσ 32:1╢ witΦ ▒M┬ oµ RA═ anΣ onσ use≥ ì
loggeΣ on¼ thσ timσ fo≥ ╬ ╜ 100░ wa≤ 5▒ sec.